home *** CD-ROM | disk | FTP | other *** search
/ X User Tools / X User Tools (O'Reilly and Associates)(1994).ISO / sources / xedit / util.c < prev    next >
C/C++ Source or Header  |  1994-09-27  |  4KB  |  162 lines

  1. #if (!defined(lint) && !defined(SABER))
  2. static char Xrcsid[] = "$XConsortium: util.c,v 1.14 89/10/07 14:59:43 kit Exp $";
  3. #endif /* lint && SABER */
  4.  
  5. /*
  6.  *              COPYRIGHT 1987
  7.  *           DIGITAL EQUIPMENT CORPORATION
  8.  *               MAYNARD, MASSACHUSETTS
  9.  *            ALL RIGHTS RESERVED.
  10.  *
  11.  * THE INFORMATION IN THIS SOFTWARE IS SUBJECT TO CHANGE WITHOUT NOTICE AND
  12.  * SHOULD NOT BE CONSTRUED AS A COMMITMENT BY DIGITAL EQUIPMENT CORPORATION.
  13.  * DIGITAL MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF THIS SOFTWARE FOR
  14.  * ANY PURPOSE.  IT IS SUPPLIED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
  15.  *
  16.  * IF THE SOFTWARE IS MODIFIED IN A MANNER CREATING DERIVATIVE COPYRIGHT RIGHTS,
  17.  * APPROPRIATE LEGENDS MAY BE PLACED ON THE DERIVATIVE WORK IN ADDITION TO THAT
  18.  * SET FORTH ABOVE.
  19.  *
  20.  *
  21.  * Permission to use, copy, modify, and distribute this software and its
  22.  * documentation for any purpose and without fee is hereby granted, provided
  23.  * that the above copyright notice appear in all copies and that both that
  24.  * copyright notice and this permission notice appear in supporting
  25.  * documentation, and that the name of Digital Equipment Corporation not be 
  26.  * used in advertising or publicity pertaining to distribution of the software
  27.  * without specific, written prior permission.
  28.  */
  29.  
  30. #include <stdio.h>
  31. #include "xedit.h"
  32.  
  33. #include <X11/Xos.h>        /* for types.h */
  34. #include <sys/stat.h>
  35.  
  36. extern Widget messwidget;
  37.  
  38. void
  39. XeditPrintf(str)
  40. char * str;
  41. {
  42.   XawTextBlock text;
  43.   static XawTextPosition pos = 0;
  44.  
  45.   text.length = strlen(str);
  46.   text.ptr = str;
  47.   text.firstPos = 0;
  48.   text.format = FMT8BIT;
  49.  
  50.   XawTextReplace( messwidget, pos, pos, &text);
  51.  
  52.   pos += text.length;
  53.   XawTextSetInsertionPoint(messwidget, pos);
  54. }
  55.  
  56. Widget
  57. MakeCommandButton(box, name, function)
  58. Widget box;
  59. char *name;
  60. XtCallbackProc function;
  61. {
  62.   Widget w = XtCreateManagedWidget(name, commandWidgetClass, box, NULL, ZERO);
  63.   if (function != NULL)
  64.     XtAddCallback(w, XtNcallback, function, (caddr_t) NULL);
  65.   return w;
  66. }
  67.  
  68. Widget 
  69. MakeStringBox(parent, name, string)
  70. Widget parent;
  71. String name, string;
  72. {
  73.   Arg args[5];
  74.   Cardinal numargs = 0;
  75.   Widget StringW;
  76.  
  77.   XtSetArg(args[numargs], XtNeditType, XawtextEdit); numargs++;
  78.   XtSetArg(args[numargs], XtNstring, string); numargs++;
  79.  
  80.   StringW = XtCreateManagedWidget(name, asciiTextWidgetClass, 
  81.                   parent, args, numargs);
  82.   return(StringW);  
  83. }
  84.  
  85. /*    Function Name: GetString
  86.  *    Description: retrieves the string from a asciiText widget.
  87.  *    Arguments: w - the ascii text widget.
  88.  *    Returns: the filename.
  89.  */
  90.  
  91. String
  92. GetString(w)
  93. Widget w;
  94. {
  95.   String str;
  96.   Arg arglist[1];
  97.   
  98.   XtSetArg(arglist[0], XtNstring, &str);
  99.   XtGetValues( w, arglist, ONE);
  100.   return(str);
  101. }
  102.  
  103. /*    Function Name: MaybeCreateFile
  104.  *    Description: Checks to see if file exists, and if no creates it.
  105.  *    Arguments: file - name of file to check.
  106.  *    Returns: none.
  107.  */
  108.  
  109. FileAccess
  110. MaybeCreateFile(file)
  111. char * file;
  112. {
  113.     Boolean exists;
  114.  
  115. /*
  116.  * If file doesn't exit create it.
  117.  */
  118.  
  119.     if (access(file, F_OK) != 0) 
  120.     creat(file, 0777);
  121.  
  122.     return(CheckFilePermissions(file, &exists));
  123. }
  124.  
  125. /*    Function Name: MaybeCreateFile
  126.  *    Description: Checks to see if file exists, and if no creates it.
  127.  *    Arguments: file - name of file to check.
  128.  *    Returns: none.
  129.  */
  130.  
  131. FileAccess
  132. CheckFilePermissions(file, exists)
  133. char * file;
  134. Boolean *exists;
  135. {
  136.     char temp[BUFSIZ], *ptr;
  137.  
  138.     if (access(file, F_OK) == 0) {
  139.     *exists = TRUE;
  140.  
  141.     if (access(file, R_OK) != 0) 
  142.         return(NO_READ);
  143.     
  144.     if (access(file, R_OK | W_OK) == 0) 
  145.         return(WRITE_OK);
  146.     return(READ_OK);
  147.     }
  148.  
  149.     *exists = FALSE;
  150.     
  151.     strcpy(temp, file);
  152.     if ( (ptr = rindex(temp, '/')) == NULL) 
  153.     strcpy(temp, ".");
  154.     else 
  155.     *ptr = '\0';
  156.     
  157.     if (access(temp, R_OK | W_OK | X_OK) == 0)
  158.     return(WRITE_OK);
  159.     return(NO_READ);
  160. }
  161.  
  162.